package com.think.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexValidate {

    public static final Pattern ALPHABETIC = Pattern.compile("[a-zA-Z]+");

    // 首字母汉字
    public static final Pattern CHINESE = Pattern.compile("^[\u4e00-\u9fa5]");

    // 英文字母开头,数字结尾
    public static final Pattern FIRST_EN_END_CN = Pattern.compile("^[A-Za-z]+[0-9]+$");

    // 所有字母是英文
    public static final Pattern ALL_EN = Pattern.compile("^[A-Za-z]+$");

    // 数字规则
    public static final Pattern NUMBER = Pattern.compile("[0-9]+");

    // 用于定义分区字母匹配
    public static final Pattern PARTITION = Pattern.compile("^[a-z0-9]");

    // 用于首字符是字母或数字
    public static final Pattern FIRST_IS_WORD_OR_NUM = Pattern.compile("^[A-Za-z0-9]");

    // 用于首字符是字母或数字
    public static final Pattern END_IS_WORD_OR_NUM = Pattern.compile("[A-Za-z0-9]$");

    // 定义首字母为大写字母
    public static final Pattern BIG_WORD = Pattern.compile("[A-Z]");

    // 英文开头可以包含空格
    public static final Pattern EN_NAME = Pattern.compile("^[A-Za-z\\s]+$");

    // 验证是否含有^%&',);=?$\"等字符："[^%&',);=?$\x22]+"
    public static final Pattern SPEC_CHAR = Pattern.compile("[`~!#$%^&*()+=|{}':);',//[//]<>/?~！#￥%……&*（）——+|{}【】‘；：”“’。，、？]+");

    // 验证文件路径是否为绝对路径
    public static final Pattern ABSOLUTE_FILE_PATH = Pattern.compile("^\\w:");

    // 验证文件名是否有后缀名
    public static final Pattern FILE_SUFFIX = Pattern.compile("\\.\\w+$");

    public static final Pattern BEGIN_NUM = Pattern.compile("^[\\d][\\d\\D]*");

    public static final Pattern EXPLORER = Pattern.compile("[\\d\\D]*");

    //日期验证
    public static final Pattern DATE = Pattern.compile("((^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(10|12|0?[13578])([-\\/\\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(11|0?[469])([-\\/\\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._])(0?2)([-\\/\\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([3579][26]00)([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][0][48])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][0][48])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][2468][048])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][2468][048])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([1][89][13579][26])([-\\/\\._])(0?2)([-\\/\\._])(29)$)|(^([2-9][0-9][13579][26])([-\\/\\._])(0?2)([-\\/\\._])(29)$))");

    /***以下是字符串正则,用于组合正则***/
    public static final String IDENTIFIER = "[a-zA-Z_$][\\w$]*";        //单独使用时,前面需加限制 ^
    public static final String PART = "[\\n\\s]";
    public static final String PART_NAME = "(" + IDENTIFIER + "\\." + PART + "*)*" + IDENTIFIER;

    public static final Pattern HTML = Pattern.compile("<[^>]*>", Pattern.CASE_INSENSITIVE);

    /**
     * 功能:正则验证
     * @param value
     * @param pattern
     * @return
     */
    public static boolean validate(String value, Pattern pattern) {
        if (value == null){
            return false;
        }
        return pattern.matcher(value).find();
    }

    /**
     * 功能:正则提取字符串
     * @param value
     * @param pattern
     * @return
     */
    public static String[] match(String value, Pattern pattern) {
        if (value == null)
            return null;
        Matcher matcher = pattern.matcher(value);
        if (matcher.find()) { //修改 bug at 2016/10/14
            int count = matcher.groupCount();
            if (count == 0) {
                //零组表示整个模式。它不包括在此计数中。 但又匹配到了,所以返回整个模式匹配串 修改 bug at 2016/10/14
                return new String[]{matcher.group()};
            }
            String[] groups = new String[count];
            for (int j = 0; j < count; j++)
                groups[j] = matcher.group(j + 1);
            return groups;
        }
        return null;
    }

    /**
     * 统计 字符串中包含正则表达示匹配的元素个数
     * @param content
     * @param pattern
     * @return 个数
     */
    public static long countRegex(String content, Pattern pattern) {
        if (StringUtils.isBlank(content)) {
            return 0;
        }
        Matcher matcher = pattern.matcher(content);
        long count = 0;
        while (matcher.find()) {
            count++;
        }
        return count;
    }

}
